var util = require('../../utils/util.js');
var api = require('../../config/api.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    showCoupon: [],
  },

  listPage: {
    page: 1,
    pageSize: 0,
    totalSize: 0,
    curpage: 1
  },
  //领取优惠券
  GotCoupon: function (e) {
    //gain_coupon
    var data = {
      couponId: '',
      couponSecretCode: '',
      couponSecretPassword: ''
    }
    data.couponId = e.currentTarget.dataset.id
    console.log(data)
    var that = this
    var customIndex = app.AddClientUrl("/gain_coupon.html", data, 'post')
    wx.request({
      url: customIndex.url,
      header: app.headerPost,
      data: customIndex.params,
      method: 'POST',
      success: function (res) {
        console.log('---------s---------')
        console.log(res.data)
        wx.showToast({
          title: '领取成功',
          icon: 'success',
          duration: 1000
        })

        that.freshData(data.couponId)

      },
      fail: function (res) {
        wx.hideLoading()
        app.loadFail()
      }
    })
  },
  freshData: function (id) {
    var showCoupon = this.data.showCoupon
    for (let i = 0; i < showCoupon.length; i++) {
      if (showCoupon[i].id == id) {
        showCoupon[i].userGotCoupon.push('1')
      }
    }

    this.setData({ showCoupon: showCoupon })

  },

  formatTime:function(number) {  
    var n = number;
    var date = new Date(n);
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();  
    return (Y + M + D);
  },

  /* 去掉日期的时间 */
  spliceData: function (e) {
    for (let i = 0; i < e.length; i++) {
      console.log(this.formatTime(e[i].startDate));
      e[i].startDate = this.formatTime(e[i].startDate)
      e[i].endDate = this.formatTime(e[i].endDate)
    }
    return e;
  },

  getNewCouponsList: function () {
    var that = this
    var getParam = {}
    getParam.page = that.listPage.page
    //getParam.page = that.listPage.page
    //var customIndex = app.AddClientUrl(api.CouponList, getParam)

   // util.request(api.CouponList, getParam).then(function (res) {
    //})

    wx.request({
      url: api.CouponList,
      header: app.header,
      data: getParam,
      success: function (res) {
        console.log(res)
        console.log(res.data.result.pageSize)
        that.listPage.pageSize = res.data.result.pageSize
        that.listPage.curPage = res.data.result.pageNum
        that.listPage.totalSize = res.data.result.total
        let dataArr = that.data.showCoupon
        dataArr = dataArr.concat(res.data.result.list)

        var result = that.spliceData(dataArr)
        that.setData({ showCoupon: result })
      },
      fail: function (res) {
        wx.hideLoading()
        app.loadFail()
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getNewCouponsList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.setData({ setting: app.setting })
    this.setData({ loginUser: app.loginUser })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.data.showCoupon = []

    this.listPage.page = 1
    this.getNewCouponsList();

    wx.stopPullDownRefresh()
  },

  
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    if (that.listPage.totalSize > that.listPage.curPage * that.listPage.pageSize) {
      that.listPage.page++
      this.getNewCouponsList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
